package com.gp.shaw.netty.nio.im;

import com.alibaba.fastjson.JSON;
import com.gp.shaw.netty.nio.im.model.IMMsg;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * Created by asus on 2018/11/7.
 */
public class SendMsg {
    public static final String MSG_END="&&end";
    public static void send(SocketChannel channel,IMMsg msg){
        if(msg == null){
            return;
        }
        String jsonStr = JSON.toJSONString(msg)+MSG_END;
        try {
            ByteBuffer byteBuffer = ByteBuffer.wrap(jsonStr.getBytes("UTF-8"));
//            byteBuffer.flip();
            channel.write(byteBuffer);
            byteBuffer.clear();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
